package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

public class Library {

    private List<LibraryItem> listOfAllBooks = new ArrayList<LibraryItem>() {{
        add(new Book("Head First Java", "Kathy Sierra", 2000));
        add(new Book("Java Programming Language", "James Gosling", 2002));
        add(new Book("Thinking in Java", "Bruce Eckel", 2004));
    }};

    private List<IssuedItem> listOfCheckedOutBooks = new ArrayList<IssuedItem>() {{
        //add(new Book("Head First Java","Kathy Sierra",2000));
        //add(new Book("Thinking in Java","Bruce Eckel",2004));
    }};

    private List<LibraryItem> listOfAllMovies = new ArrayList<LibraryItem>() {{
        add(new Movie("m1", "d1", 2000, 9));
        add(new Movie("m2", "d2", 2002, 8));
        add(new Movie("m3", "d3", 2004, 7));
    }};

    private List<IssuedItem> listOfCheckedOutMovies = new ArrayList<IssuedItem>();

    public List<LibraryItem> getAvailableBookList() {

        List<LibraryItem> listOfAvailableBooks = new ArrayList<LibraryItem>();
        for (LibraryItem listOfAllBook : listOfAllBooks) {
            if (!checkAvailableInCheckedOutList(listOfCheckedOutBooks,listOfAllBook)) {
                listOfAvailableBooks.add(listOfAllBook);
            }
        }
        return listOfAvailableBooks;
    }

    public List<LibraryItem> getAvailableMovieList() {

        List<LibraryItem> listOfAvailableMovies = new ArrayList<LibraryItem>();
        for (LibraryItem listOfAllMovie : listOfAllMovies) {
            if (!checkAvailableInCheckedOutList(listOfCheckedOutMovies,listOfAllMovie)) {
                listOfAvailableMovies.add(listOfAllMovie);
            }
        }
        return listOfAvailableMovies;
    }

    private boolean checkAvailableInLibrary(List<LibraryItem> libraryItemList, LibraryItem libraryItem) {

        for (int i = 0; i < libraryItemList.size(); i++) {
            if (libraryItemList.get(i).equals(libraryItem)) {
                return true;
            }
        }
        return false;
    }

    private boolean checkAvailableInCheckedOutList(List<IssuedItem> libraryItemList, LibraryItem libraryItem) {

        for (int i = 0; i < libraryItemList.size(); i++) {

            if (libraryItemList.get(i).getLibraryItem().equals(libraryItem) && libraryItemList.get(i).getLogin() != null) {
                return true;
            }
        }
        return false;
    }

    List<LibraryItem> getAllItems(LibraryItem libraryItem) {
        if (libraryItem instanceof Book) {
            return listOfAllBooks;
        } else {
            return listOfAllMovies;
        }
    }

    List<IssuedItem> getAllCheckedOutItems(LibraryItem libraryItem) {
        if (libraryItem instanceof Book) {
            return listOfCheckedOutBooks;
        } else {
            return listOfCheckedOutMovies;
        }
    }

    boolean checkOut(LibraryItem libraryItem) {
        List<LibraryItem> libraryItemAllList;
        List<IssuedItem> libraryItemCheckedOutList;
        IssuedItem issuedItem;

        libraryItemAllList = getAllItems(libraryItem);
        libraryItemCheckedOutList = getAllCheckedOutItems(libraryItem);

        issuedItem = new IssuedItem(libraryItem, LoginValidator.getCurrentUser());

        if (checkAvailableInLibrary(libraryItemAllList, libraryItem)) {
            if ( libraryItemCheckedOutList.size() == 0) {
                libraryItemCheckedOutList.add(issuedItem);
                return true;
            } else {
                for (int i = 0; i < libraryItemCheckedOutList.size(); i++) {
                    if ( !libraryItemCheckedOutList.get(i).getLibraryItem().equals(libraryItem) && libraryItemCheckedOutList.get(i).getLogin() != null) {
                        libraryItemCheckedOutList.add(issuedItem);
                        return true;
                    }
                }
            }
        }
        return false;
    }

    boolean returnItem(LibraryItem libraryItem) {

        List<LibraryItem> libraryItemAllList;
        List<IssuedItem> libraryItemCheckedOutList;

        libraryItemAllList = getAllItems(libraryItem);
        libraryItemCheckedOutList = getAllCheckedOutItems(libraryItem);

        if (checkAvailableInLibrary(libraryItemAllList, libraryItem)) {

            for (int i = 0; i < libraryItemCheckedOutList.size(); i++) {
                if (libraryItemCheckedOutList.get(i).getLibraryItem().equals(libraryItem) && libraryItemCheckedOutList.get(i).getLogin().equals(LoginValidator.getCurrentUser())) {
                    libraryItemCheckedOutList.remove(libraryItemCheckedOutList.get(i));
                    return true;
                }
            }
        }

        return false;
    }

}

